package com.wyt.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wyt.controller.util.Data;
import com.wyt.mapper.UserMapper;
import com.wyt.pojo.Book;
import com.wyt.pojo.Borrow;
import com.wyt.pojo.User;
import com.wyt.service.BookService;
import com.wyt.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import javax.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;

@Controller
public class StudentController {

    private static final Logger log = LoggerFactory.getLogger(StudentController.class);

    @Autowired
    private BookService bookService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;


    @GetMapping("/stu/index")
    public String stuIndex(){
//        return "redirect:/stu/index.html";
        return "stu/index.html";
    }

    @GetMapping("/stu/stu/home")
    public String stuHome(){
        return "stu/home.html";
    }

    @GetMapping("/stu/admin/borrow")
    public String stuBorrow(){
        return "redirect:/stu/borrow.html";
    }

    @GetMapping("/stu/admin/restore")
    public String stuRestore(){
        return "redirect:/stu/restore.html";
    }
    @PostMapping("/index/stu/getBookInfo")
    @ResponseBody
    public Data getBookInfo(HttpSession session, String ID){
        Data data = new Data();
        String stuID=(String)session.getAttribute("stu.ID");
        if (stuID==null){
            data.setCode("-1");
            data.setMessage("请先登录");
        }else {
            Book book = bookService.getById(ID);
            if (book==null){
                data.setCode("0");
                data.setMessage("查无此书");
            }else {
                User user = userService.getUserById(stuID);
                Integer user_now_num = 0;
                if (user.getBook1Id()==null){
                    user_now_num++;
                }
                if (user.getBook2Id()==null){
                    user_now_num++;
                }

                Borrow borrow = new Borrow();
                borrow.setID(book.getId());
                borrow.setName(book.getName());
                borrow.setPublisher(book.getPublisher());
                borrow.setUser_now_num(user_now_num);
                borrow.setBook_now_num(book.getTotalNum()- book.getBorrowedNum());

                data.setCode("1");
                data.setData(borrow);
            }
        }
        return data;
    }
    @PostMapping("/index/stu/borrowBook")
    @ResponseBody
    public Data borrowBook(Borrow borrow,HttpSession session){
        String stuID = (String)session.getAttribute("stu.ID");
        Data data = new Data();
        if (stuID==null||stuID.equals("")){
            data.setCode("-1");
            data.setMessage("请先登录");
        }else {
//            书本不存在时：{code:0, message:"查无此书"} 有点多余
            if (borrow.getBook_now_num()==0){
                data.setCode("0");
                data.setMessage("此书已被借完");
            }else {
                if (borrow.getUser_now_num()==0){
                    data.setCode("0");
                    data.setMessage("您的借书额度已用完");
                }else {
                    //更新书籍信息
                    Book book = bookService.getById(borrow.getID());
                    book.setBorrowedNum(book.getBorrowedNum()+1);
                    book.setTotalNum(book.getTotalNum());
                    QueryWrapper<Book> queryWrapper = new QueryWrapper();
                    queryWrapper.eq("ID",borrow.getID());
                    bookService.update(book,queryWrapper);
                    //更新用户信息
                    User user = userService.getUserById(stuID);
                    if (user.getBook1Id()==null){
                        userMapper.updateUserBook(stuID, Integer.parseInt(borrow.getID()),null);
                    }else {
                        userMapper.updateUserBook(stuID, user.getBook1Id(), Integer.parseInt(borrow.getID()));
                    }
                    data.setCode("1");
                    data.setMessage("借书成功");
                }
            }
        }
        return data;
    }

    @RequestMapping("/index/stu/getBorrowInfo")
    @ResponseBody
    public Data getBorrowInfo(HttpSession session){
        Data data = new Data();
        String stuID =(String) session.getAttribute("stu.ID");
        User user = userMapper.getUserById(stuID);
        ArrayList<Book> list = new ArrayList<>();
        if (stuID==null){
            data.setCode("-1");
            data.setMessage("请先登录");
        }else {
            Book book1 = bookService.getById(user.getBook1Id());
            Book book2 = bookService.getById(user.getBook2Id());
            list.add(book1);
            list.add(book2);
            data.setCode("1");
            data.setData(list);
        }
        return data;
    }
    @RequestMapping("/index/stu/restoreBook")
    @ResponseBody
    public Data restoreBook(HttpSession session,String id){
        Data data = new Data();
        String stuID = (String) session.getAttribute("stu.ID");
        User user = userService.getUserById(stuID);
        Book book = bookService.getById(id);
        if(stuID==null){
            data.setCode("-1");
            data.setMessage("请先登录");
        }else {
            if(id.equals(user.getBook1Id()+"")){
                user.setBook1Id(user.getBook2Id());
                user.setBook2Id(null);
            }else {
                user.setBook2Id(null);
            }
            userMapper.updateUserBook(stuID,user.getBook1Id(),user.getBook2Id());
            book.setBorrowedNum(book.getBorrowedNum()-1);
            QueryWrapper<Book> queryWrapper = new QueryWrapper();
            queryWrapper.eq("ID",book.getId());
            bookService.update(book,queryWrapper);
            data.setCode("1");
            data.setMessage("还书成功");
        }
        return data;
    }
}
